
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter_ahj_app/app/changePassword/ChangePasswordViewModel/ChangePasswordViewModel.dart';
import 'package:flutter_ahj_app/base/basewidget/view/BaseWidget.dart';
import 'package:flutter_ahj_app/base/utils/JUIKit/InputView.dart';
import 'package:flutter_ahj_app/base/utils/JUIKit/JFlatButton.dart';
import 'package:flutter_ahj_app/base/utils/JUIKit/JPCH.dart';
import 'package:flutter_ahj_app/base/utils/ToastUtils.dart';
import 'package:flutter_ahj_app/base/utils/configurationFile/ConfigurationFile.dart';
import 'package:flutter_ahj_app/base/utils/mTopBar.dart';
import 'package:flutter_ahj_app/base/values/SQColor.dart';
import 'package:flutter_ahj_app/base/values/dimens.dart';
import 'package:flutter/services.dart';
class ChangePasswordView extends BaseStatefulWidget{
  @override
  State<StatefulWidget> initState() {
    // TODO: implement initState
    return _ChangePasswordViewSate();
  }
}

class _ChangePasswordViewSate extends BaseState<ChangePasswordViewModel,ChangePasswordView>{

  TextEditingController _originalPWDCtr = TextEditingController();///原始密码
  TextEditingController _newPWDCtr = TextEditingController();///新密码
  TextEditingController _confirmPWDCtr = TextEditingController();///确认新密码

  ///原始密码
  bool _obscureOriginalPWD = true;///是否隐藏
  ///新密码错误 信息
  String _errorText;
  bool _obscureNewPWD = true;///是否隐藏
  ///确认新密码错误 信息
  String _confirmErrorText;
  bool _obscureConfirmPWD = true;///是否隐藏

  @override
  Widget initWidget() {
    // TODO: implement initWidget
    return Scaffold(
      appBar: MTopBar(
        title: "修改密码"
      ).create(),
      body: _bodVew(),
    );
  }
  _bodVew() {
    return Column(
      children: <Widget>[
        ///原始密码
        InputView.outlineInputBorder(
          padding: EdgeInsets.fromLTRB(JScreenSpace, JSpace, JScreenSpace, 0),
          labelText: "请输入原密码",
          labelStyle: TextStyle(fontSize: Dimens.textSize28),
          obscureText: _obscureOriginalPWD,
          textEditingController: _originalPWDCtr,
          suffixIcon: IconButton(icon: _obscureOriginalPWD == true ? Icon(Icons.visibility_off) : Icon(Icons.visibility), onPressed: (){
            setState(() {
              _obscureOriginalPWD = !_obscureOriginalPWD;
            });
          }),

        ),
        ///新密码
        InputView.outlineInputBorder(
          inputFormatters: [
            FilteringTextInputFormatter.allow(RegExp("[0-9]|[a-z]|[A-Z]")),
          ],
          padding: EdgeInsets.fromLTRB(JScreenSpace, JSpace, JScreenSpace, 0),
          labelText: "请输入新密码",
          labelStyle: TextStyle(fontSize: Dimens.textSize28),
          textEditingController: _newPWDCtr,
          errorText: _errorText,
          obscureText: _obscureNewPWD,
          onChanged: ((value){

            String errorText;

            if (6 > value.length) {
              errorText =  "密码不能少于6位";
            } else if (value.length > 12) {
              errorText = "密码不能大于12位";
            }

            setState(() {
              _errorText = errorText;
            });

          }),

          suffixIcon: IconButton(icon: _obscureNewPWD == true ? Icon(Icons.visibility_off) : Icon(Icons.visibility), onPressed: (){

            setState(() {
              _obscureNewPWD = !_obscureNewPWD;
            });
          }),
          
        ),
        ///确认新密码
        InputView.outlineInputBorder(
          inputFormatters: [
            FilteringTextInputFormatter.allow(RegExp("[0-9]|[a-z]|[A-Z]")),
          ],
          padding: EdgeInsets.fromLTRB(JScreenSpace, JSpace, JScreenSpace, 0),
          labelText: "重复输入新密码",
          labelStyle: TextStyle(fontSize: Dimens.textSize28),
          textEditingController: _confirmPWDCtr,
          errorText: _confirmErrorText,
          obscureText: _obscureConfirmPWD,
          onChanged: ((value){

            String confirmErrorText;

//            if (6 > value.length) {
//              confirmErrorText =  "密码不能少于6位";
//            } else if (value.length > 12) {
//              confirmErrorText = "密码不能大于12位";
//            }

            if(_newPWDCtr.text != value){
              confirmErrorText = "两次输入的新密码不一致";
            }

            setState(() {
              _confirmErrorText = confirmErrorText;
            });

          }),
          suffixIcon: IconButton(icon: _obscureConfirmPWD == true ? Icon(Icons.visibility_off) : Icon(Icons.visibility), onPressed: (){
            setState(() {
              _obscureConfirmPWD = !_obscureConfirmPWD;

            });
          }),
        ),
        SizedBox(height: 50,),
        ///确认修改
        JFlatButton(
          title: "确认修改",
            color: JPch.primaryColor(context),
            frameMargin: EdgeInsets.all(15),
            onPressed: () => _confirmAction(),
        )
      ],
    );
  }
  ///判断是否输入
  _judgeEmpty() {

    if(_originalPWDCtr.text==""){
      ToastUtils.showToast("请输入原密码");
      return true;
    }
    if(_newPWDCtr.text==""){
      ToastUtils.showToast("请输入新密码");
      return true;
    }
    if(_confirmPWDCtr.text==""){
      ToastUtils.showToast("请重复输入新密码");
      return true;
    }else{

      if(_newPWDCtr.text != _confirmPWDCtr.text){
        ToastUtils.showToast("两次输入的新密码不一致");
        return true;
      }
      if (6 > _newPWDCtr.text.length) {
        ToastUtils.showToast("密码不能少于6位");
        return true;
      } else if (_newPWDCtr.text.length > 12) {
        ToastUtils.showToast("密码不能大于12位");
        return true;
      }

    }

    return false;
  }
  ///确认按钮
  _confirmAction(){

    if(_judgeEmpty()){
      return;
    }

    String oldPassword = _originalPWDCtr.text;
    String newPassword = _confirmPWDCtr.text;
    viewModel.requestMethod(oldPassword, newPassword);
    viewModel.dataStream().listen((result){

      String _msg = isEmpty(result["msg"])==true ? "返回数据出错" : result["msg"];
      ToastUtils.showToast(_msg);

      if(result["code"] == 0) {
        Future.delayed(Duration(seconds: 1),(){
          Navigator.pop(context);
        });
      }

    });
    print("确认按钮");
  }
}